using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Activities.Rules;
using System.CodeDom;
using System.Collections;
using NVelocityTemplateEngine.Interfaces;
using System.Reflection;
using NVelocityTemplateEngine;
using System.Xml;

namespace EventAbstractionAPI.RuleEngine
{
	public class SeqEvent:IComplexEvent
	{
        /// <summary>
        /// Given the use case event algebra E1 = E2;E3
        /// E1 is the resulting complex event of the sequence of E2 then E3.
        /// E1 would be the ComplexEvent Parameter that would go into this method.
        /// E2 and E3 would be strings put into the events list. As events is a parameter,
        /// it is assumed there is the ability to enter many events of no limit.
        /// </summary>
        /// <param name="ComplexEvent">Name of the complex event.</param>
        /// <param name="events">The events in cronological order that in sequence are the resulting ComplexEvent</param>
        public SeqEvent(TimeSpan timeToLive, String complexEvent, params String[] events)
        {
            _timeToLive = timeToLive;
            _complexEventName = complexEvent;
            _events = events;
        }

        #region ComplexEvent Members

        public XmlDocument Rule
        {
            get
            {
                string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;

                INVelocityEngine embeddedEngine = NVelocityEngineFactory.CreateNVelocityAssemblyEngine(assemblyName,"EventAbstractionAPI.RuleEngine", true);

                IDictionary context = new Hashtable();

                context.Add("ComplexEvent", _complexEventName);
                context.Add("Name", _complexEventName);

                int count = 1;
                foreach (String eventType in _events)
                {
                    context.Add("EventType" + count.ToString(), eventType);
                    count++;
                }

                String xmlString = embeddedEngine.Process(context, "Resources.SequenceRule.vm");

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlString);

                return doc;
            }

            set
            {
                //we may require set later.
                ;
            }
        }

        public TimeSpan TimeOfLife
        {
            get
            {
                return _timeToLive;
            }
            set
            {
                _timeToLive = value;
            }
        }

        public string FirstParameter
        {
            get
            {
                return _events[0];
            }
        }
        #endregion

        #region Member Variables

        TimeSpan _timeToLive;
        String _complexEventName;
        String[] _events;

        #endregion
    }
}
/*
 * Example: 
 * 
 * Condition
 *  this.enumerator.Current.ToString() == "eventType1" && this.value3[this.index + 1].ToString() == "eventType3"
 * 
 * Action
 * this.value3.RemoveRange(this.index, 2)
 * this.value3.Insert(this.index, "eventType4")
 * this.enumerator = this.value3.GetEnumerator()
 * this.index = -1
 * 
*/